package cn.edu.zafu.bigdata.x.xiejunjie.databaseoperations;

import java.sql.*;
import java.util.*;

public class StudentManagementSystem {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    searchStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    deleteStudent();
                    break;
                case 5:
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效的选项，请重新输入。");
                    break;
            }
        }
    }

    private static void addStudent() {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();

        try (Connection conn = DatabaseConnection.getConnection();
             PreparedStatement ps = conn.prepareStatement("INSERT INTO student_0226 (id, name, phone) VALUES (?, ?, ?)")) {
            ps.setString(1, id);
            ps.setString(2, name);
            ps.setString(3, phone);

            int rowsAffected = ps.executeUpdate();

            if (rowsAffected > 0) {
                System.out.println("学生信息添加成功！");
            } else {
                System.out.println("学号重复，请重新输入。");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库操作失败：" + e.getMessage());
        }
    }

    private static void searchStudent() {
        while (true) {
            System.out.print("请输入学号进行检索（输入'QUIT'退出）: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }

            try (Connection conn = DatabaseConnection.getConnection();
                 PreparedStatement ps = conn.prepareStatement("SELECT * FROM student_0226 WHERE id = ?")) {
                ps.setString(1, id);

                ResultSet rs = ps.executeQuery();

                if (rs.next()) {
                    String name = rs.getString("name");
                    String phone = rs.getString("phone");
                    System.out.println("学号: " + id + ", 姓名: " + name + ", 电话: " + phone);
                } else {
                    System.out.println("未找到该学生");
                }
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("数据库操作失败：" + e.getMessage());
            }
        }
    }

    private static void updateStudent() {
        while (true) {
            System.out.print("请输入学号进行更新（输入'QUIT'退出）: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }

            System.out.print("请输入新的姓名: ");
            String name = scanner.nextLine();
            System.out.print("请输入新的电话: ");
            String phone = scanner.nextLine();

            try (Connection conn = DatabaseConnection.getConnection();
                 PreparedStatement ps = conn.prepareStatement("UPDATE student_0226 SET name = ?, phone = ? WHERE id = ?")) {
                ps.setString(1, name);
                ps.setString(2, phone);
                ps.setString(3, id);

                int rowsAffected = ps.executeUpdate();

                if (rowsAffected > 0) {
                    System.out.println("学生信息更新成功！");
                } else {
                    System.out.println("未找到该学生");
                }
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("数据库操作失败：" + e.getMessage());
            }
        }
    }

    private static void deleteStudent() {
        while (true) {
            System.out.print("请输入学号进行删除（输入'QUIT'退出）: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }

            try (Connection conn = DatabaseConnection.getConnection();
                 PreparedStatement ps = conn.prepareStatement("DELETE FROM student_0226 WHERE id = ?")) {
                ps.setString(1, id);

                int rowsAffected = ps.executeUpdate();

                if (rowsAffected > 0) {
                    System.out.println("学生信息删除成功！");
                } else {
                    System.out.println("未找到该学生");
                }
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("数据库操作失败：" + e.getMessage());
            }
        }
    }
}
